package com.xinzhi.savingpot.task.service.impl;

import com.xinzhi.savingpot.task.entity.TaskInfo;
import com.xinzhi.savingpot.task.mapper.TaskEnrollMapper;
import com.xinzhi.savingpot.task.service.ITaskEnrollService;
import com.xinzhi.savingpot.util.BaseResponse;
import com.xinzhi.savingpot.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class TaskEnrollServiceImpl implements ITaskEnrollService {

    @Autowired
    private TaskEnrollMapper enrollMapper;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public BaseResponse EnrollTask(String taskNumber, Integer userId) {

        if (taskNumber.isEmpty() || userId == null) {
            return new BaseResponse(400, "任务编号不能为空");
        }
        TaskInfo ti = enrollMapper.queryTaskRemainPlaces(taskNumber);
        if (ti.getRemainPlaces()<=0) {
            return new BaseResponse(400, "该任务剩余数量不足，无法领取");
        }
        if (ti.getEndTime().getTime() <= System.currentTimeMillis()){
            return new BaseResponse(400, "任务已结束");
        }
        int i = enrollMapper.UserEnrollerTask(taskNumber, userId, new Date());
        if (i>0) {
            return new BaseResponse(200, "任务领取成功");
        } else {
            return new BaseResponse(400, "任务领取失败");
        }


    }
}